package com.sq.partner.manager.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;

import com.sq.partner.manager.CacheManager;
import com.sq.partner.manager.ConnectorManager;
import com.sq.partner.manager.LogManager;
import com.sq.partner.manager.MonitorManager;
import com.sq.partner.manager.common.MonitorAppCache;
import com.sq.partner.manager.mybatis.model.TransactionMonitoring;
import com.sq.partner.manager.service.IGatewayClearCacheManager;
import com.sq.partner.manager.util.LogUtil;
import com.sq.partner.manager.util.Page;
@Scope("prototype")
public class GatewayClearCacheManagerImpl implements IGatewayClearCacheManager {

	private   List<MonitorManager> monitorManagerList;

	private Logger logger = LogUtil.getLog(GatewayClearCacheManagerImpl.class);
	@Override
	public String clearCache(String appId,String ip) throws Exception{
		monitorManagerList=MonitorAppCache.getCache();
		if(appId!=null&&!"".equals(appId)){
			if(monitorManagerList!=null&&monitorManagerList.size()>0){
				for(MonitorManager mm:monitorManagerList){
					String appid = mm.getAppId();
					if(appid != null && appid.equals(appId)){
						for(CacheManager cm:mm.getCacheManager()){
							if(cm.getName() != null && "ChannelInfo".equals(cm.getName())){
								cm.clearAllCache(ip);
								return "成功";
							}
						}
					}
				}
			}
		}
		return "失败";
	}
	@Override
	public Page<TransactionMonitoring> cacheInfoList(String monitorServiceIndex, String serviceIp) {
		Page<TransactionMonitoring> page =new Page<TransactionMonitoring>();
		
		
		List<TransactionMonitoring> list=new ArrayList<TransactionMonitoring>();
		monitorManagerList=MonitorAppCache.getCache();
			List<CacheManager> cachelist=monitorManagerList.get(Integer.parseInt(monitorServiceIndex)).getCacheManager();
			if(cachelist!=null){
				for(int i=0;i<cachelist.size();i++){
					TransactionMonitoring tm=new TransactionMonitoring();
					 if(cachelist.get(i)==null){
						 tm.setCacheName("不匹配");
						 list.add(tm);
						 break;
					 }
					 tm.setCacheName(cachelist.get(i).getName());
					 tm.setCacheindex(i);
					 list.add(tm);
				}
			}
		page.setData(list);
		return page;
	}
	@Override
	public Page<TransactionMonitoring> queryAppname() {
		List<TransactionMonitoring> list=new ArrayList<TransactionMonitoring>();
		Page<TransactionMonitoring> page=new Page<TransactionMonitoring>();
		monitorManagerList=MonitorAppCache.getCache();
		if(null != monitorManagerList){
			for(int i=0;i<monitorManagerList.size();i++){
				 TransactionMonitoring tm=new TransactionMonitoring();
				 tm.setCacheName(monitorManagerList.get(i).getName());
				 tm.setConnindex(i);
				 tm.setAppId(monitorManagerList.get(i).getAppId());
				 list.add(tm);
			}
			page.setData(list);
			return page;
		}
		return null;
	}
	@Override
	public Page<TransactionMonitoring> getServiceIp(String appId) {
		List<TransactionMonitoring> iplist=new ArrayList<TransactionMonitoring>();

		List<CacheManager> cmlist = new ArrayList<CacheManager>();
		
		try {
			monitorManagerList=MonitorAppCache.getMonitor(appId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.info(e);
		}
		for(MonitorManager mm:monitorManagerList){
			String appid = mm.getAppId();
			if(appid != null && appid.equals(appId)){
				cmlist = mm.getCacheManager();
				break;
			}
		}
		Page<TransactionMonitoring> page=null;
		if(cmlist!=null){
			page=new Page<TransactionMonitoring>();	
				Map<String,String> ipMap = cmlist.get(0).getServiceURL();
				for(Map.Entry<String, String> ip:ipMap.entrySet()){
					 TransactionMonitoring tm=new TransactionMonitoring();
					 tm.setServiceIp(ip.getKey());
					 iplist.add(tm);
				}
				page.setData(iplist);
		}
		return page;
	}
	@Override
	public void clearCacheInfoManager(int monitorServiceIndex, String serviceIp, int cacheindex) throws Exception {
		monitorManagerList=MonitorAppCache.getCache();
		monitorManagerList.get(monitorServiceIndex).getCacheManager().get(cacheindex).clearAllCache(serviceIp);
	}
	
	/**
	 * 清除新意健险缓存
	 * @param monitorServiceIndex
	 * @param serviceIp
	 * @param cacheindex
	 * @throws Exception
	 */
	@Override
	public void clearNlifeCacheInfoManager() throws Exception {
		monitorManagerList=MonitorAppCache.getCache();
		MonitorManager monitorManager = new MonitorManager();
		for(int i = 0;i<monitorManagerList.size();i++){
			if("nlife".equals(monitorManagerList.get(i).getAppId())){
				monitorManager = monitorManagerList.get(i);
				break;
			}				
		}
		List<CacheManager> cacheManager = monitorManager.getCacheManager();
		for(int j = 0; j<cacheManager.size();j++){
			if("ChannelInfo".equals(cacheManager.get(j).getName())){
				cacheManager.get(j).clearAllCache("muleserver");
			}
		}
	}
	
	@Override
	public Page<TransactionMonitoring> connStatusList(
			Integer monitorServiceIndex, String serviceIp) throws Exception {
		Page<TransactionMonitoring> page =new Page<TransactionMonitoring>();
		
		List<TransactionMonitoring> connlist=new ArrayList<TransactionMonitoring>();
		monitorManagerList=MonitorAppCache.getCache();
		List<ConnectorManager> cmlist=monitorManagerList.get(monitorServiceIndex).getConnectorManager();
		if(cmlist!=null){
			for(int i=0;i<cmlist.size();i++){
				List<String> list=cmlist.get(i).getStatus(serviceIp);
				String s = null;
				boolean b=true;
				if(list.size()==1){
					s=list.get(0);
					if(s==null){
						connlist=null;
						break;
					}
				}else{
					for(int j=0;j<list.size();j++){
						if(list.get(j)==null){
							s="不匹配";
							break;
						}else{
							if(b){
								s=list.get(j);
								b=false;
							}else{
								if(s!=null&&s.equals(list.get(j))){
									s=list.get(j);
								}else{
									s="不匹配";
									break;
								}
							}
						}
					}
				}
				 TransactionMonitoring tm=new TransactionMonitoring();
				 tm.setConnName(cmlist.get(i).getName());
				 tm.setConnStauts(s);
				 tm.setConnindex(i);
				 connlist.add(tm);
			}
		}
		page.setData(connlist);
		
		return page;
	}
	@Override
	public void  operatConn(Integer monitorServiceIndex,
			String serviceIp,Integer connIndex, String connstatus) throws Exception {
			monitorManagerList=MonitorAppCache.getCache();
			if("started".equals(connstatus))
			{
				monitorManagerList.get(monitorServiceIndex).getConnectorManager().get(connIndex).stop(serviceIp);
				
			}else if("stopped".equals(connstatus)){
				monitorManagerList.get(monitorServiceIndex).getConnectorManager().get(connIndex).start(serviceIp);
			}
	}
	@Override
	public Page<TransactionMonitoring> logManagerList(
			Integer monitorServiceIndex, String serviceIp) throws Exception {
Page<TransactionMonitoring> page=new Page<TransactionMonitoring>();
		List<TransactionMonitoring> list=new ArrayList<TransactionMonitoring>();
		monitorManagerList=MonitorAppCache.getCache();
		  int ifIndex=monitorServiceIndex;
		  if(ifIndex !=-1){
			LogManager lm=monitorManagerList.get(ifIndex).getLogManager();
			if(lm!=null){
				List<Map<String,String>> list2=lm.getLoggers("LEVELED",serviceIp);
			
					Set<String> key=new HashSet<String>();
					for(int i=0;i<list2.size();i++){
						Map<String,String> map=list2.get(i);
						if(map!=null){
							for(Map.Entry<String,String> m:map.entrySet()){
								key.add(m.getKey());
							}
						}
					}
					for(String s:key){
						boolean b = true;
						String value=null;
						TransactionMonitoring tm=new TransactionMonitoring();
						for(int m=0;m<list2.size();m++){
							Map<String,String> map=list2.get(m);
							if(map==null){
								value="不匹配";
								break;
							}else{
								if(b){
									value=map.get(s);
									b = false;
								}else{
									if(null != value && value.equals(map.get(s))){ //
										value = map.get(s);
									}else{
										value="不匹配";
										break;
									}
									
								}
							}
						}
						tm.setLogName(s);
						tm.setLogLeven(value);
						list.add(tm);
					}
				}
			}
		  
		  page.setData(list);
		return page;

	}
	@Override
	public void updateLogLeven(Integer monitorServiceIndex, String serviceIp,
			String logName, String logLeven) throws Exception {
		monitorManagerList=MonitorAppCache.getCache();

			if(logLeven.equals("INFO"))
			{
				monitorManagerList.get(monitorServiceIndex).getLogManager().assignInfoLevel(logName,serviceIp);
	
			}else if(logLeven.equals("DEBUG"))
			{
				monitorManagerList.get(monitorServiceIndex).getLogManager().assignDebugLevel(logName,serviceIp);
	
				
			}else if(logLeven.equals("WARN"))
			{
				monitorManagerList.get(monitorServiceIndex).getLogManager().assignWarnLevel(logName,serviceIp);

			}else if(logLeven.equals("ERROR"))
			{
				monitorManagerList.get(monitorServiceIndex).getLogManager().assignErrorLevel(logName,serviceIp);
			
				
			}else if(logLeven.equals("TRACE"))
			{
				monitorManagerList.get(monitorServiceIndex).getLogManager().assignTraceLevel(logName,serviceIp);
		
			}
	}
	@Override
	public void resetLog(Integer monitorServiceIndex, String serviceIp) throws Exception {
		monitorManagerList=MonitorAppCache.getCache();
		monitorManagerList.get(monitorServiceIndex).getLogManager().resetConfiguration(serviceIp);
	}
	@Override
	public void clearAllCache(String appId) throws Exception {
		monitorManagerList=MonitorAppCache.getCache();
		for(MonitorManager mm:monitorManagerList){
			String appid = mm.getAppId();
			if(appid != null && appid.equals(appId)){
				for(CacheManager cm:mm.getCacheManager()){
					if(cm.getName() != null && "ChannelInfo".equals(cm.getName())){
						cm.clearAllCache("");
					}
				}
			}
		}
		MonitorAppCache.refresh();
	}
	@Override
	public void clearAllCache()  {
		 List<MonitorManager> monitorManagerList=MonitorAppCache.getCache();
		for(int n =0;n<monitorManagerList.size() ;n++){
			String appid = monitorManagerList.get(n).getAppId();
			if(appid != null ){
				 List<CacheManager> cm=monitorManagerList.get(n).getCacheManager();
				for(int i=0;i<cm.size();i++){
					if(cm.get(i).getName() != null && "ChannelInfo".equals(cm.get(i).getName())){
						try {
							cm.get(i).clearAllCache("");
							MonitorAppCache.refresh();
						} catch (Exception e) {
							// TODO Auto-generated catch block
							logger.error("缓存清理失败");
						}
					}
				}
			}
		}
		
	}
	/**
	 * 清除信保缓存
	 * @param monitorServiceIndex
	 * @param serviceIp
	 * @param cacheindex
	 * @throws Exception
	 */
	@Override
	public void clearCreditCacheInfoManager() throws Exception {
		monitorManagerList=MonitorAppCache.getCache();
		MonitorManager monitorManager = new MonitorManager();
		for(int i = 0;i<monitorManagerList.size();i++){
			if("credit".equals(monitorManagerList.get(i).getAppId())){
				monitorManager = monitorManagerList.get(i);
				break;
			}				
		}
		List<CacheManager> cacheManager = monitorManager.getCacheManager();
		for(int j = 0; j<cacheManager.size();j++){
			if("ChannelInfo".equals(cacheManager.get(j).getName())){
				cacheManager.get(j).clearAllCache("muleserver");
			}
		}
	}
	
}
